<?php
namespace app\index\home;
use app\common\controller\Common;
use think\Db;
use yuexiu\Client;
use yuexiu\Wx;
use think\facade\Session;
use app\common\model\Access;
class Index extends Common
{
    public function test(){
        Session::set("user",[
            "id"=>23,
            "userId"=>"oSrroww0YYeGLv5fMsE7ywJ5I3yo",
            "area"=>"11hMO8ja",
            "nickname"=>"冰和iceand"
        ]);
        $this->redirect(get_domain()."/index.html?helpid=");
    }

    public function index(){
        $fb_user_id = input('get.fb_user_id/s');
        if(!$fb_user_id){
            errMsg("参数错误");
        }
        $client = new Client();
        $asedata = openssl_decrypt(base64_decode($fb_user_id),  "AES-128-CBC", $client->appId, OPENSSL_RAW_DATA, $client->appSecret);
        $asearr = explode("|",$asedata);

        $userId = $asearr[0];
        if(time()>$asearr[1]){
//            errMsg("密钥已过期");
        }

        $userCheck = Db::name('userinfo')->where('userId',$userId)->value('id');
        $suser = input('get.suser/d');//分享的用户ID
        $area = input('get.organizationCode/s');  //区域ID
        $poster = input('get.poster/d');  //海报ID
        $visitorStatus = input('get.visitorStatus/d');  //用户用户身份
        $area = str_replace("organization_","",$area);

        $result = $client->getuserinfo($userId);

        if(!$userCheck){


            $data['userId'] = $result['userId'];
            $data['avatarUrl'] = $result['avatarUrl'];
            $data['gender'] = $result['gender'];
            $data['phone'] = isset($result['phone'])?$result['phone']:null;
            $data['createdAt'] = $result['createdAt'];
            $data['visitorStatus'] = $result['visitorStatus'];
            if($data['visitorStatus']){
                $data['visitorStatus'] = implode(',',$data['visitorStatus']);
            }
            $data['unionId'] = $result['unionId'];
            $data['publicStatus'] = $result['publicStatus'];
            $data['publicOpenId'] = isset($result['publicOpenId'])?$result['publicOpenId']:"";
            $data['nickname'] = $result['nickname'];
            $data['ctime'] = time();
            $newuser_insert = Db::name('userinfo')->insertGetId($data);
            if($newuser_insert){
                $return_data['userId'] = $data['userId'];
                $return_data['avatarUrl'] = $data['avatarUrl'];
                Session::set("user",[
                    "id"=>$newuser_insert,
                    "userId"=>$data['userId'],
                    "area"=>$area,
                    "nickname"=>$result['nickname']
                ]);
                (new Access())->add([
                    "userid"=>$newuser_insert,
                    "suser"=>$suser,
                    "organizationCode"=>$area,
                    "poster"=>$poster,
                    "visitorStatus"=>$visitorStatus,
                    "phone"=>$data['phone'],
                    "newStatus"=>$result["newStatus"],
                ]);
                $this->redirect(get_domain()."/index.html?helpid=".$suser);
            }
            else{
                errMsg('error');
            }
        }else{
            Session::set("user",[
                "id"=>$userCheck,
                "userId"=>$userId,
                "area"=>$area,
                "nickname"=>$result['nickname']
            ]);
            if($suser == $userCheck){
                $suser = null;
            }
            $data=[];
            $data['publicStatus'] = $result['publicStatus'];
            $data['publicOpenId'] = isset($result['publicOpenId'])?$result['publicOpenId']:"";
            $data['phone'] = isset($result['phone'])?$result['phone']:null;
            Db::name('userinfo')->where("id",$userCheck)->update($data);
            (new Access())->add([
                "userid"=>$userCheck,
                "suser"=>$suser,
                "organizationCode"=>$area,
                "poster"=>$poster,
                "visitorStatus"=>$visitorStatus,
                "phone"=>isset($result['phone'])?$result['phone']:null,
                "newStatus"=>$result["newStatus"],
            ]);
            $this->redirect(get_domain()."/index.html?helpid=".$suser);
        }
    }
}
